const mysql = require('mysql');
const fs = require('fs');
const readline = require('readline');

// 数据库配置
const connection = mysql.createConnection({
	host:     '39.106.160.61',
	user:     'root',
	password: '123',
	database: 'test'
});

// 连接数据库
connection.connect();
// 定义sql语句
const sentence_sql = 'INSERT INTO sentence(content, location) VALUE(?, ?)';
const w_s_sql = 'INSERT INTO word_sentence(word_id, sentence_id) VALUE(?, ?)';
const query_w_sql = 'SELECT FROM word WHERE english = ?';


// 建立文件读取流
const rs = fs.createReadStream('sentence_file', 'utf-8');
// readline, 按行读取文件
const rl = readline.createInterface({
	input: rs
});

let sentence_id = 1;
rl.on('line', (line) => {
	[location, content] = line.split('<&&>');
	// sql 语句参数赋值
	let sqlParams = [content, location];

	// 执行insert语句，向数据库插入数据
  	connection.query(sentence_sql, sqlParams, (err, result) => {
		if (err) {
			console.log('[INSERT ERROR] - ', err.message);
			return;
		}
	});
});

// 释放资源
rs.on('end', () => {
	console.log('--------END--------')
	connection.end();
	rl.close();
});




